﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GzipManagerPro.UI.Utility;

namespace GzipManagerPro.UI.Controls
{
    public class Explorer : BrightIdeasSoftware.TreeListView
    {
        public Explorer()
        {
            this.FullRowSelect = true;
            this.HeaderUsesThemes = true;
            this.HideSelection = false;
            this.ShowImagesOnSubItems = true;
            this.ShowItemToolTips = true;
            this.View = System.Windows.Forms.View.Details;
        }

        protected override void OnDoubleClick(EventArgs e)
        {
            if(this.SelectedItem != null)
            {
                object selectedItem = this.SelectedItem.RowObject;

                if (selectedItem is ArchiveFileSystemInfo)
                {
                    ArchiveFileSystemInfo info = selectedItem as ArchiveFileSystemInfo;
                    if (info.IsDirectory)
                    {
                        this.Expand(selectedItem);
                        return;
                    }
                }
                else if (selectedItem is CustomFileSystemInfo)
                {
                    CustomFileSystemInfo info = selectedItem as CustomFileSystemInfo;
                    if (info.IsDirectory)
                    {
                        this.Expand(selectedItem);
                        return;
                    }
                }
            }
            base.OnDoubleClick(e);
        }

        public string FormatFileSize(long size)
        {
            int[] limits = new int[] { 1024 * 1024 * 1024, 1024 * 1024, 1024 };
            string[] units = new string[] { "GB", "MB", "KB" };

            for (int i = 0; i < limits.Length; i++)
            {
                if (size >= limits[i])
                    return String.Format("{0:#,##0.##} " + units[i], ((double)size / limits[i]));
            }

            return String.Format("{0} bytes", size);
            ;
        }

        private IExplorerProvider _Provider;
        public IExplorerProvider Provider
        {
            get
            {
                return _Provider;
            }
            set
            {
                _Provider = value;

                if (value != null)
                {
                    try
                    {
                        ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
                        value.Initialize(this);
                    }
                    catch
                    {
                    }
                    finally
                    {
                        ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
                    }
                }
            }
        }

        
    }
}
